﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyeye.Car.Net.UI.SystemAdmin;
using Aliyeye.Car.Net.UI.SystemHelp;
using Aliyeye.Car.Net.UI.AccountsUsersModel;
using Aliyeye.Car.Net.UI.OrderModel;
using Aliyeye.Car.Net.UI.CarModel;
using Aliyeye.Car.Net.UI.StatisticsModel;
using Aliyeye.Car.Net.UI.Common;
using Aliyeye.Car.Net.UI.SystemModel;

namespace Aliyeye.Car.Net.UI
{
    public partial class MainWin : Form
    {
        private static MainWin _instance = new MainWin();
        private int msgXIndex = 0;

        public static MainWin GetInstance()
        {
            if (null != _instance)
                return _instance;
            return new MainWin();
        }

        public MainWin()
        {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor |
                ControlStyles.DoubleBuffer | ControlStyles.UserPaint |
                ControlStyles.AllPaintingInWmPaint, true);
            UpdateStyles();

            InitializeComponent();
            msgXIndex = lblRemind.Location.X;

            if (Login.CustomID != 0)
            {
                //客户登陆
                panelRemind.Visible = false;
                //刷新按钮禁止
                smallIconButton3.Enabled = false;
            }
        }

        private void ShowClientForm(Form driverInfo)
        {
            this.SuspendLayout();
            closeChildForm();
            driverInfo.Load += new EventHandler(HandleDriverInfoLoad);
            driverInfo.MdiParent = this;
            driverInfo.WindowState = FormWindowState.Maximized;
            //driverInfo.ControlBox = false;
            driverInfo.Show();
            this.ResumeLayout();
        }

        private void HandleDriverInfoLoad(object sender, EventArgs e)
        {
            Form frm = sender as Form;

            if (frm == null)
            {
                return;
            }
            frm.WindowState = FormWindowState.Maximized;
            frm.ControlBox = false;
            //frm.MaximizeBox = false;
            //frm.MinimizeBox = false;
        }

        private void ShowForm(Form form)
        {
            form.ShowDialog();
        }

        private void CtronEnble()
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.AccountsUsersSvc.SoapHeaderClass();
            AccountsUsersSvc.Accounts_Users model = new Aliyeye.Car.Net.UI.AccountsUsersSvc.Accounts_Users();
            int userID = Login.LogUserId;
            ServiceManipulator.SafeAsyncExecution<AccountsUsersSvc.Accounts_UsersServiceSoapClient>(
                proxy => proxy.GetModel(sopaHeader, userID),
                result =>
                {
                    if (result.Error == null)
                    {
                        model = (AccountsUsersSvc.Accounts_Users)result.Result;
                        if (model != null)
                        {
                            DataSet ds = null;
                            string strWhere = " CategoryID=" + model.RoleId;
                            Accounts_PermissionsSvc.SoapHeaderClass sopaHeaderper = new Aliyeye.Car.Net.UI.Accounts_PermissionsSvc.SoapHeaderClass();
                            ServiceManipulator.SafeAsyncExecution<Accounts_PermissionsSvc.Accounts_PermissionsServiceSoapClient>(
                                 proxy1 => proxy1.GetList(sopaHeaderper, strWhere),
                                result1 =>
                                {
                                    if (result.Error == null)
                                    {
                                        ds = (DataSet)result1.Result;
                                        if (ds.Tables[0].Rows.Count > 0)
                                        {
                                            for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                                            {
                                                if (tsbSystemAdmin.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    tsbSystemAdmin.Enabled = true;
                                                }

                                                if (toolStripButton2.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton2.Enabled = true;
                                                }

                                                if (toolStripButton3.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton3.Enabled = true;
                                                }

                                                if (toolStripButton4.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton4.Enabled = true;
                                                }

                                                if (toolStripButton5.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton5.Enabled = true;
                                                }

                                                if (toolStripButton7.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton7.Enabled = true;
                                                }

                                                if (linkLabel1.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel1.Enabled = true;
                                                }

                                                if (linkLabel2.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel2.Enabled = true;
                                                }

                                                if (linkLabel7.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel7.Enabled = true;
                                                }

                                                if (linkLabel8.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel8.Enabled = true;
                                                }

                                                if (toolStripButton6.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton6.Enabled = true;
                                                }

                                                if (toolStripButton1.Text == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    toolStripButton1.Enabled = true;
                                                }

                                                if (linkLabel9.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel9.Enabled = true;
                                                }

                                                if (linkLabel11.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel11.Enabled = true;
                                                }

                                                if (linkLabel12.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel12.Enabled = true;
                                                }

                                                if (linkLabel10.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel10.Enabled = true;
                                                }

                                                if (linkLabel14.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel14.Enabled = true;
                                                }

                                                if (linkLabel6.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel6.Enabled = true;
                                                }

                                                if (linkLabel5.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel5.Enabled = true;
                                                }

                                                if (linkLabel13.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel13.Enabled = true;
                                                }

                                                if (linkLabel4.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel4.Enabled = true;
                                                }

                                                if (linkLabel3.Text.Trim() == ds.Tables[0].Rows[j]["Description"].ToString())
                                                {
                                                    linkLabel3.Enabled = true;
                                                }

                                            }
                                        }
                                        else
                                        {
                                            DisableAllMenu();
                                        }
                                    }
                                    else
                                    {
                                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                                    }
                                },
                                this
                            );
                        }
                        overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                    else
                    {
                        PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                    }
                },
                this
            );
        }

        private void DisableAllMenu()
        {
            linkLabel3.Enabled = false;
            linkLabel4.Enabled = false;
            linkLabel13.Enabled = false;
            linkLabel5.Enabled = false;
            linkLabel6.Enabled = false;
            linkLabel12.Enabled = false;
            linkLabel10.Enabled = false;
            linkLabel14.Enabled = false;
            linkLabel11.Enabled = false;
            linkLabel9.Enabled = false;
            toolStripButton1.Enabled = false;
            toolStripButton6.Enabled = false;
            linkLabel8.Enabled = false;
            linkLabel7.Enabled = false;
            linkLabel2.Enabled = false;
            linkLabel1.Enabled = false;
            toolStripButton5.Enabled = false;
            toolStripButton4.Enabled = false;
            toolStripButton3.Enabled = false;
            toolStripButton2.Enabled = false;
            tsbSystemAdmin.Enabled = false;
            toolStripButton7.Enabled = false;
        }

        //关闭所有子窗口
        private void closeChildForm()
        {
            for (int x = 0; x < this.MdiChildren.Length; x++)
            {
                Form tempChild = (Form)this.MdiChildren[x];
                tempChild.Close();
            }
        }

        private void MainWin_Load(object sender, EventArgs e)
        {
            lblUserName.Text = Login.LogTrueName;
            ShowCompanyInfo();
            if (Login.CustomID == 0)
                ShowRemind();
            DisableAllMenu();  // 禁止所有菜单
            CtronEnble();//角色判断
            WelcomeForm frm = new WelcomeForm();
            ShowClientForm(frm);
        }

        private void ShowRemind()
        {
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            CarSvc.SoapHeaderClass sopaHeader = new CarSvc.SoapHeaderClass();
            CarSvc.RemindListInfo[] list = null;
            ServiceManipulator.SafeAsyncExecution<CarSvc.CarServiceSoapClient>(
                            proxy => proxy.GetRemindList(sopaHeader),
                            result =>
                            {
                                if (result.Error == null)
                                {
                                    list = (CarSvc.RemindListInfo[])result.Result;
                                    if (list.Length > 0)
                                    {
                                        panelRemind.Visible = true;
                                        timeRemind.Enabled = true;
                                    }
                                    else
                                    {
                                        panelRemind.Visible = false;
                                        timeRemind.Enabled = false;
                                    }
                                    overlay.FadeOut();Cursor.Current = Cursors.Default;
                                }
                                else
                                {
                                    //MessageBox.Show(result.Error.Message + "------------" + result.Error.StackTrace);
                                    PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                                }
                            },
                            this
                        );
        }

        private void ShowCompanyInfo()
        {
            CompanySvc.SoapHeaderClass sopaHeader = new CompanySvc.SoapHeaderClass();
            DataSet ds = null;
            string strCompany = "";
            overlay.FadeIn();Cursor.Current = Cursors.WaitCursor;
            ServiceManipulator.SafeAsyncExecution<CompanySvc.CompanyServiceSoapClient>(
                    proxy => proxy.GetList(sopaHeader, ""),
                    result =>
                    {
                        if (result.Error == null)
                        {
                            ds = (DataSet)result.Result;
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                strCompany = ds.Tables[0].Rows[0]["ComName"].ToString() + "管理系统";
                                this.Text = strCompany;
                                this.lblTitle.Text = strCompany;
                            }
                            overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                        else
                        {
                            PageCommon.Error(result.Error.Message); overlay.FadeOut();Cursor.Current = Cursors.Default;
                        }
                    },
                    this
                );
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            string strClickItem = e.ClickedItem.Text;
            if (this.Cursor == Cursors.WaitCursor) return;
            Cursor = Cursors.WaitCursor;

            switch (strClickItem)
            {
                case "首页":
                    ShowClientForm(new WelcomeForm());
                    break;
            }
            Cursor = Cursors.Arrow;
        }

        //系统管理
        private void tsbSystemAdmin_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminMain());
        }
        //车辆管理
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminWork());
        }
        //驾驶员管理
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminBusiness());
        }
        //财务统计
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminStatistics());
        }
        //订车管理
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminHelp());
        }
        //修改密码
        private void smallIconButton5_Click(object sender, EventArgs e)
        {
            ShowClientForm(new MakePass());
        }

        //车辆建档
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Login.IsTestUser)
            {
                CarSvc.CarServiceSoapClient client = new Aliyeye.Car.Net.UI.CarSvc.CarServiceSoapClient();
                CarSvc.SoapHeaderClass sopaHeader = new Aliyeye.Car.Net.UI.CarSvc.SoapHeaderClass();
                DataSet ds = client.GetList(sopaHeader, "");
                if (ds != null)
                {
                    if (ds.Tables[0].Rows.Count >= 5)
                    {
                        PageCommon.Alert("已超过5部车，请连络QQ ：539217021！");
                        return;
                    }
                }
            }
            ShowClientForm(new AddCarsNew());
        }
        //车辆信息
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new CarList());
        }

        //按驾驶员统计
        private void linkLabel7_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Login.IsTestUser)
            {
                PageCommon.Alert(Login.TestUserMessage);
                return;
            }
            ShowClientForm(new StatisticsByDriver());
        }
        // 驾驶员明细
        private void linkLabel8_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new UserList(true));
        }
        //重新登陆
        private void smallIconButton4_Click(object sender, EventArgs e)
        {
            ((System.Windows.Forms.Form)this.Tag).Visible = true;
            this.Close();
        }

        //首页
        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            ShowClientForm(new WelcomeForm());
        }

        //结算补款
        private void linkLabel10_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new SettlementRenewalList());
        }

        //退出-关闭窗口
        private void MainWin_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (((System.Windows.Forms.Form)this.Tag).Visible == false)
            {
                Application.Exit();
            }
        }
        //员工管理
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminEmployees());
        }
        //客户管理
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ShowClientForm(new SystemAdminCustomer());
        }

        //驾驶员建档
        private void linkLabel9_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new AddUsers(true));
        }

        //按客户统计
        private void linkLabel11_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Login.IsTestUser)
            {
                PageCommon.Alert(Login.TestUserMessage);
                return;
            }
            ShowClientForm(new StatisticsByCustomer());
        }

        //所有订单
        private void linkLabel12_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Login.IsTestUser)
            {
                PageCommon.Alert(Login.TestUserMessage);
                return;
            }
            ShowClientForm(new AllOrdersList());
        }

        //长租带驾预订
        private void linkLabel6_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new LognLeaseWithDriverList());
        }

        //短租带驾预订
        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new ShortLeaseWithDriverList());
        }

        //长租自驾预订
        private void linkLabel13_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShortLeaseWithOutDriverList form = new ShortLeaseWithOutDriverList(false);
            form.Text = "长租自驾预定";
            form.toolStripLabel1.Text = "长租自驾预定";
            ShowClientForm(form);
        }

        //短租自驾预订
        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new ShortLeaseWithOutDriverList(true));
        }

        //客户名单
        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new Aliyeye.Car.Net.UI.AdminCustomer.Customer());
        }

        private void timeRemind_Tick(object sender, EventArgs e)
        {
            lblRemind.Location = new Point(lblRemind.Location.X - 1, lblRemind.Location.Y);
            if (lblRemind.Location.X == -228)
                lblRemind.Location = new Point(285, lblRemind.Location.Y);
        }

        private void lblRemind_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            RemindList per = new RemindList();
            per.ShowDialog();
        }

        //全国报价
        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.828car.cn/car");
        }

        //版权声明
        private void smallIconButton1_Click(object sender, EventArgs e)
        {
            CopyrightStatement show = new CopyrightStatement();
            show.ShowDialog();
        }

        //常见问题
        private void smallIconButton2_Click(object sender, EventArgs e)
        {
            FrequentlyQuestions show = new FrequentlyQuestions();
            show.ShowDialog();
        }

        private void smallIconButton3_Click(object sender, EventArgs e)
        {
            if (Login.CustomID == 0)
                ShowRemind();
        }

        //取车
        private void linkLabel14_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ShowClientForm(new GetCarList());
        }

        //汽车促销
        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.828car.cn/carsale");
        }

        //保险优惠
        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.828car.cn/safe");
        }
    }
}
